package edu.hsbremen.teamscore.activities;

import java.util.Random;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import edu.hsbremen.teamscore.R;

public class ScreenTennis extends Activity {
	
	Button scorePlayer1;
	Button scorePlayer2;
	RadioButton aufschlagSpieler1;
	RadioButton aufschlagSpieler2;
	RadioButton advantageTeam1;
	RadioButton advantageTeam2;
	TextView pointsPlayer1;
	TextView pointsPlayer2;
	TextView satz1Player1;
	TextView satz2Player1;
	TextView satz3Player1;
	TextView satz1Player2;
	TextView satz2Player2;
	TextView satz3Player2;
	TextView scoreTeam1;
	TextView scoreTeam2;
	
	int satzpl11;
	int satzpl12;
	int satzpl13;
	int satzpl21;
	int satzpl22;
	int satzpl23;
	int serve;
	
	String winner;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tennis);
	
		//initialize all UI elemnts
		scorePlayer1 = (Button) findViewById(R.id.tennis_score_team1);
		scorePlayer2 = (Button) findViewById(R.id.tennis_score_team2);
		aufschlagSpieler1 = (RadioButton) findViewById(R.id.tennis_aufschlag_team1);
		aufschlagSpieler2 = (RadioButton) findViewById(R.id.tennis_aufschlag_team2);
		advantageTeam1 = (RadioButton) findViewById(R.id.tennis_adv_team1);
		advantageTeam2 = (RadioButton) findViewById(R.id.tennis_adv_team2);
		pointsPlayer1 = (TextView) findViewById(R.id.tennis_txt_team1);
		pointsPlayer2 = (TextView) findViewById(R.id.tennis_txt_team2);
		satz1Player1 = (TextView) findViewById(R.id.tennis_satz1_team1);
		satz2Player1 = (TextView) findViewById(R.id.tennis_satz2_team1);
		satz3Player1 = (TextView) findViewById(R.id.tennis_satz3_team1);
		satz1Player2 = (TextView) findViewById(R.id.tennis_satz1_team2);
		satz2Player2 = (TextView) findViewById(R.id.tennis_satz2_team2);
		satz3Player2 = (TextView) findViewById(R.id.tennis_satz3_team2);
		scoreTeam1 = (TextView) findViewById(R.id.tennis_satz_score1);
		scoreTeam2 = (TextView) findViewById(R.id.tennis_satz_score2);
		scoreTeam1.setText("0");
		scoreTeam2.setText("0");
		
		// Receive the intent from enternames to fill in the correct Names of the teams
		Bundle namesBundle = getIntent().getExtras();
		pointsPlayer1.setText(namesBundle.getString("team1"));
		pointsPlayer2.setText(namesBundle.getString("team2"));
		
		
		// Choose randomly who is serving teh first play
		whoServesFirst();
		
		
		// register the Click listeners
		scorePlayer1.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//incrementScoreTeam1();	
				incrementScoreTeam(0, scoreTeam1, scoreTeam2, advantageTeam1, advantageTeam2);
			}
		});
		
		scorePlayer2.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//incrementScoreTeam2();
				incrementScoreTeam(1, scoreTeam2, scoreTeam1, advantageTeam2, advantageTeam1);
			}
		});
	}
	
	private void incrementScoreTeam(int myTeamIndex, TextView myScore, TextView opponentScore, RadioButton myAdvantage, RadioButton opponentAdvantage) {
		String satzStand1 = (String) myScore.getText();
		String satzStand2 = (String) opponentScore.getText();
		
		if (satzStand1.equals("0")){
			myScore.setText("15");
		} else if(satzStand1.equals("15")){
			myScore.setText("30");
		} else if(satzStand2.equals("40") && satzStand1.equals("30")){
			Toast toast = Toast.makeText(ScreenTennis.this, "DEUCE", Toast.LENGTH_SHORT);
			toast.show();
			myScore.setText("40");
		} else if(satzStand1.equals("30")){
			myScore.setText("40");
		} else if (satzStand1.equals("40") && satzStand2.equals("40") && myAdvantage.isChecked()){
			//incrementSatzTeam1();
			incrementSatz(myTeamIndex);
			changeServe();
			checkWinner();
			myScore.setText("0");
			myAdvantage.setChecked(false);
		} else if(satzStand2.equals("40") && satzStand1.equals("40") && opponentAdvantage.isChecked()){
			Toast toast = Toast.makeText(ScreenTennis.this, "DEUCE", Toast.LENGTH_SHORT);
			toast.show();
			myAdvantage.setChecked(false);
			opponentAdvantage.setChecked(false);
		} else if(satzStand1.equals("40") && satzStand2.equals("40") && !myAdvantage.isChecked() && !opponentAdvantage.isChecked()){
			myAdvantage.setChecked(true);
		}else if(satzStand1.equals("40") && !satzStand2.equals("40")){
			//incrementSatzTeam1();
			incrementSatz(myTeamIndex);
			changeServe();
			checkWinner();
			myScore.setText("0");
		}
	}
	
	public void incrementSatz(int team) {
		
		int setNr = 1;
				
		TextView[] setT1 = new TextView[] {satz1Player1, satz2Player1, satz3Player1 };
		
		TextView[] setT2 = new TextView[] {satz1Player2, satz2Player2, satz3Player2 };
		
		TextView[][] teamSets = new TextView[][] { setT1, setT2 };
		
		for (int i = 0; i < 3; i++) {
			if (Integer.parseInt((String) teamSets[0][i].getText()) == 6 || Integer.parseInt((String) teamSets[1][i].getText()) == 6) {
				setNr++;
			}
		}
		
		if(Integer.parseInt((String) teamSets[team][0].getText()) < 6 && setNr == 1){
			teamSets[team][0].setText(Integer.toString(Integer.parseInt((String)teamSets[team][0].getText()) + 1));
		} else if(Integer.parseInt((String) teamSets[team][1].getText()) < 6 && setNr == 2){
			teamSets[team][1].setText(Integer.toString(Integer.parseInt((String)teamSets[team][1].getText()) + 1));
		} else if (Integer.parseInt((String) teamSets[team][2].getText()) < 6 && setNr == 3){
			teamSets[team][2].setText(Integer.toString(Integer.parseInt((String)teamSets[team][2].getText()) + 1));
		}		
		
		scoreTeam1.setText("0");
		scoreTeam2.setText("0");
	}
	
	/**
	 * Checks who has won the whole match
	 */
	public void checkWinner(){
		int satzpl11 = Integer.parseInt((String) satz1Player1.getText());
		int satzpl12 = Integer.parseInt((String) satz2Player1.getText());
		int satzpl13 = Integer.parseInt((String) satz3Player1.getText());
		int satzpl21 = Integer.parseInt((String) satz1Player2.getText());
		int satzpl22 = Integer.parseInt((String) satz2Player2.getText());
		int satzpl23 = Integer.parseInt((String) satz3Player2.getText());
		if (satzpl11 + satzpl12 == 12 || satzpl11 + satzpl13 == 12 || satzpl12 + satzpl13 == 12){
			winner = pointsPlayer1.getText().toString();
			Toast toast = Toast.makeText(ScreenTennis.this, "And the winner is " + winner, Toast.LENGTH_LONG);
			toast.show();
		} else if(satzpl21 + satzpl22 == 12 || satzpl21 + satzpl23 == 12 || satzpl22 + satzpl23 == 12){
			winner = pointsPlayer2.getText().toString();
			Toast toast = Toast.makeText(ScreenTennis.this, "And the winner is " + winner, Toast.LENGTH_LONG);
			toast.show();
		}
	}
	
	/**
	 * Random choose who is serving first
	 */
	public void whoServesFirst(){
		Random random = new Random(SystemClock.elapsedRealtime());
		serve = random.nextInt(100);
		Log.d("Random Nummer", "" + serve);
		if (serve < 50){
			serve = 0;
			aufschlagSpieler1.setChecked(true);
		} else{
			serve = 1;
			aufschlagSpieler2.setChecked(true);
		}
	}
	
	/**
	 * Chnages the Display Radiobuttons for displaying who has to serve
	 */
	public void changeServe(){
		if (serve == 1){
			serve = 0;
			aufschlagSpieler1.setChecked(true);
			aufschlagSpieler2.setChecked(false);

		} else {
			serve = 1;
			aufschlagSpieler2.setChecked(true);
			aufschlagSpieler1.setChecked(false);

		}
	}
	
	
	/**
	 * Onm create method to create the Menu Bar
	 * @param Menu menu
	 * @return boolean = true
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.menu_tennis, menu);
	    return true;
	}
	
	/**
	 * Click listener for the menu bar
	 * @param MenuItem item
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    switch (item.getItemId()) {
	    case R.id.tennis_rules:
	    	Intent Rules = new Intent(ScreenTennis.this, Descriptions.class);
	    	Rules.putExtra("game", "tennis");
	    	startActivity(Rules);
	    	return true;
	    case R.id.tennis_backtomain:
	    	this.finish();
	    	return true;
	    default:
	        return super.onOptionsItemSelected(item);
	    }
	}
}
